public class BinaryTree {
    public class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;
        public  TreeNode(char val) {
            this.val = val;
        }
    }
    //创建一个二叉树
    public TreeNode creatTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        A.left = B;
        A.right = C;
        B.right = D;
        C.left = E;
        C.right = F;
        return A;
    }
    /*中序遍历检查二叉树*/
    public void indexOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        indexOrder(root.left);
        System.out.print(root.val+" ");
        indexOrder(root.right);
    }
    /*求二叉树的节点个数*/
    //普通方法
    public int count;
    public int normalNode(TreeNode root) {
        if(root == null) {
            return 0;
        }
        count++;
        normalNode(root.left);
        normalNode(root.right);
        return count;
    }
    //子问题的思想
    public int numberNode(TreeNode root) {
        if(root==null){
            return 0;
        }
        return numberNode(root.right)+numberNode(root.left)+1;
    }
    /*求叶子节点的个数
     叶子节点: 度为零的节点*/
    public int count1;
    public int leafNode(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right==null) {
            count1++;
        }
        leafNode(root.left);
        leafNode(root.right);
        return count1;
    }
    //求第k层节点个数
    public int countNode(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return countNode(root.left,k-1)+countNode(root.right,k-1);
    }
    //查找节点是否存在
    public TreeNode find(TreeNode root,int key) {
        if (root == null) {
            return null;
        }
        if (root.val == key) {
            return root;
        }
        TreeNode leftVal = find(root.left, key);
        if (leftVal != null) {
            return leftVal;
        }
        TreeNode rightVal = find(root.right, key);
        if (rightVal != null) {
            return rightVal;
        }
        return null;
    }
    /*求树的高度*/
    public int getHeight (TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(leftHeight,rightHeight) +1;
    }
}
